import {ROLE_NAME,USERINFO} from '@/store/mutation-types'
import Vue from 'vue'
//随机生成32随机数 作为主键id
export function getNum32 () {
  var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  var nums = "";
  for (var i = 0; i < 32; i++) {
    var id = parseInt(Math.random() * 61);
    nums += chars[id];
  }
  return nums;
}

// 是否为空
export function isNoEmpty (info) {
  if (info !== null && info !== '' && info !== undefined) {
    return true
  } else {
    return false
  }
}

export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () { }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}


//获取当前时间日期-年月日
export function getNowFormatDate () {
  var date = new Date()
  var seperator1 = '-'
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate
  return currentdate

}

// 当前日期的上周四到这周三
export function getWeekTime () {
  var now = new Date();
  var first = 0
  var second = 0
  var startTime = 0
  var endTime = 0
  var day = now.getDay();
  if (day > 3) {
    first = day - 3 //当前时间需要减去的天数为当前周3日期（后者的时间）
    second = 6 + first
    //上面算好的时间倒退7天
    startTime = addDelectTime(-first)
    endTime = addDelectTime(-second)
  } else if (day > 0) {
    first = (3 - day) - 7 //当前时间需要减去的天数为当前周3日期（后者的时间）
    startTime = addDelectTime(first)
    endTime = addDelectTime(first - 6)
    //上面算好的时间倒退7天
  } else {
    // 周日
    startTime = addDelectTime(-5)
    endTime = addDelectTime(-11)
  }
  //当前时间 加上日期天数
  // var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"); //var weeknow = weeks[day];
  var array = []
  array = [endTime, startTime]
  return array;
}

//
function addDelectTime (iDays) {
  var date = new Date();
  date = new Date(date.setDate(date.getDate() + iDays));
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  var nowData = year + "-" + month + "-" + day;
  return nowData;
}

//时间范围计算 2526时间规则
export function getNowTime () {
  var seperator1 = '-'
  var date1 = new Date();
  var date2 = new Date();
  var date3 = new Date();
  var date4 = new Date();
  let arr = []
  if (date1.getDate() < 26) {
    date1.setMonth(date1.getMonth() - 2);
    date2.setMonth(date2.getMonth() - 1);
    let year1 = date1.getFullYear()
    let month1 = date1.getMonth() + 1
    let strDate1 = '25'
    let year2 = date2.getFullYear()
    let month2 = date2.getMonth() + 1
    let strDate2 = '26'
    var currentdate1 = year1 + seperator1 + month1 + seperator1 + strDate1
    var currentdate2 = year2 + seperator1 + month2 + seperator1 + strDate2
    arr.push(currentdate1)
    arr.push(currentdate2)
  } else {
    
    date3.setMonth(date3.getMonth() - 1);
    let year1 = date3.getFullYear()
    let month1 = date3.getMonth() + 1
    let strDate1 = '25'
    let year2 = date4.getFullYear()
    let month2 = date4.getMonth() + 1
    let strDate2 = '26'
    var currentdate1 = year1 + seperator1 + month1 + seperator1 + strDate1
    var currentdate2 = year2 + seperator1 + month2 + seperator1 + strDate2
    console.log("2222",currentdate1,currentdate2)
    arr.push(currentdate1)
    arr.push(currentdate2)
  }
  return arr
}


//个角色的权限
// 1管理员2客户经理3项目经理4代维单位5区县公司6市公司
export function getUserRight (role) {
  let userRightList = []
  switch (role) {
    case "管理员":
      userRightList = ["index", "analysis", "urgeManage", "accountManage", "resourceManage", 'resourceMangea', 'resourceMangeb', 'resourceMangec', 'resourceManged', "system"]
      break;
    case "一点支撑人员":
      userRightList = ["index", "analysis", "urgeManage", "accountManage"]
      break; 
    case "区县建设":
      userRightList = ["index", "analysis", "urgeManage"]
      break;     
    case "客户经理":
      userRightList = ["index", "analysis", "urgeManage",]
      break;
    case "项目经理":
      userRightList = ["index", "analysis", "urgeManage",]
      break;
    case "代维单位":
      userRightList = ["index", "resourceManage", 'resourceMangea', 'resourceMangec']
      break;
    case "区县公司":
      userRightList = ["index", "resourceManage", 'resourceMangea', 'resourceMangec']
      break;
    case "市公司":
      userRightList = ["index", "resourceManage", 'resourceMangea', 'resourceMangeb', 'resourceMangec', 'resourceManged',]
      break;
    default:
      userRightList = ["index"]
  }
  return userRightList
}

// 页面判断权限功能
export function getUserRightAction () {
  let userRightList = []
  let role = Vue.ls.get(ROLE_NAME)
  let belong = ''
  let id = ''
  switch (role) {
    case "管理员":
      belong = ''
      id = Vue.ls.get(USERINFO).id
      userRightList = [belong, id]
      break;
    case "客户经理":
      userRightList = []
      break;
    case "项目经理":
      userRightList = []
      break;
    case "代维单位":
      belong = Vue.ls.get(USERINFO).belong
      id = Vue.ls.get(USERINFO).id
      userRightList = [belong, id]
      break;
    case "区县公司":
      belong = Vue.ls.get(USERINFO).belong
      id = Vue.ls.get(USERINFO).id
      userRightList = [belong, id]
      break;
    case "市公司":
      belong = Vue.ls.get(USERINFO).belong
      id = Vue.ls.get(USERINFO).id
      userRightList = [belong, id]
      break;
    default:
      userRightList = []
  }
  return userRightList
}




//个十百千万 位数的获取
export function getNumBits (shi) {
  var w = parseInt(shi % 100000 / 10000)
  var q = parseInt(shi % 100000 % 10000 / 1000)
  var b = parseInt(shi % 100000 % 10000 % 1000 / 100)
  var s = parseInt(shi % 100000 % 10000 % 1000 % 100 / 10)
  var g = parseInt(shi % 100000 % 10000 % 1000 % 100 % 10 / 1)
  return [w, q, b, s, g]
}

// UTC-时间格式转换
export function renderTime (date) {
  var dateee = new Date(date).toJSON();
  return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}

export function dataFunction (dataTime) {
  var date = new Date(dataTime)
  var year = date.getFullYear()
  /* 在日期格式中，月份是从0开始的，因此要加0
   * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
   * */
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
}

//判断字符是否为空
export function isEmpty (obj) {
  return (typeof obj === 'undefined' || obj === null || obj === "");
}

/**
 * 关键信息隐藏
 * @param str 字符串
 * @param frontLen 字符串前面保留位数
 * @param endLen 字符串后面保留位数
 * @returns {string}
 */
export function hideCode (str, frontLen, endLen) {
  if (isEmpty(str)) {
    return ''
  } else {
    var len = str.length - frontLen - endLen;
    var xing = '';
    for (var i = 0; i < len; i++) {
      xing += '*';
    }
    return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
  }

};